package com.feel.mall.adminservice.service;

import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;
import com.feel.mall.adminservice.mapper.AdminMapper;
import com.feel.mall.common.bean.Admin;
import com.feel.mall.common.exception.FeelException;
import com.feel.mall.common.service.AdminService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author: 刘永钦
 * @Description:
 */
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Override
    public Admin login(String username, String password) throws FeelException {
//        根据用户名查询
        Admin admin = adminMapper.selectByUsername(username);
        if (admin == null) {
            throw new FeelException("用户不存在");
        }
//        对用户输入密码加密     使用md5算法和盐进行加密
        String md5Pwd = SecureUtil.md5(SecureUtil.md5(password + admin.getSalt()));
        // 对加密之后的密码和数据库中的密码进行比较
        if (!md5Pwd.equals(admin.getPassword())) {
            throw new FeelException("密码错误");
        }
        // 判断用户是否可用
        if (admin.getStatus().equals(0)) {
            throw new FeelException("账户被禁用，请联系管理员");
        }

        return admin;
    }

    @Override
    public Admin selectById(Integer id) {
        Admin admin = adminMapper.selectById(id);
        return admin;
    }

    @Override
    public void updatePwd(String oldPassword, String newPassword, Integer id) throws FeelException {
    //     数据库中查询管理员相关信息
        Admin admin = adminMapper.selectById(id);
        String salt = admin.getSalt();
        String oldPasswordMd5 = SecureUtil.md5(SecureUtil.md5(oldPassword + salt));
        if (!admin.getPassword().equals(oldPasswordMd5)) {
            throw new FeelException("密码错误请重新输入");
        }
        // 加密新密码
        String newPasswordMd5 = SecureUtil.md5(SecureUtil.md5(newPassword + salt));

        admin = new Admin();
        admin.setId(id);
        admin.setPassword(newPasswordMd5);

    //     数据库更新操作
        adminMapper.updateInfo(admin);
    }

    @Override
    public void updateInfo(Admin admin) {
        adminMapper.updateInfo(admin);
    }

    @Override
    public void resetPwd(Admin admin) {
        Admin localAdmin = adminMapper.selectById(admin.getId());
        String salt = localAdmin.getSalt();
        String newPwd = SecureUtil.md5(SecureUtil.md5("123" + salt));
        admin.setPassword(newPwd);

        adminMapper.updateInfo(admin);
    }

    @Override
    public Object getAll(Integer pageNum, Integer pageSize) {
        if (pageNum == null) {
            List<Admin> list = adminMapper.selectAll();
            return list;
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Admin> list = adminMapper.selectAll();
        PageInfo<Admin> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void addInfo(Admin admin) throws FeelException {
        // 判断账户是否存在
        Admin localAdmin = adminMapper.selectByUsername(admin.getUsername());
        if (localAdmin != null) {
            throw new FeelException("账户已存在请重新输入");
        }
    //     生成盐
        String salt = IdUtil.randomUUID().split("-")[0];
        String password = SecureUtil.md5(SecureUtil.md5("123" + salt));
    //     设置参数
        admin.setPassword(password);
        admin.setSalt(salt);
        admin.setStatus(1);
        admin.setCreateTime(new Date());

        adminMapper.addInfo(admin);
    }
}
